package com.axxc.jpa.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.axxc.jpa.annotation.MyLog;
import com.axxc.jpa.annotation.SaveOrUpdate;
import com.axxc.jpa.entity.EnglishWordNoteBookEntity;
import com.axxc.jpa.out.filter.EnglishWordNoteBookFilter;
import com.axxc.jpa.out.model.EnglishWordNoteBook;
import com.axxc.jpa.service.IEnglishWordNoteBookService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @ClassName: EnglishWordController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author xxc
 * @date 2018年8月20日 下午2:15:17
 * @see {@link https://gitee.com/biuabiu }
 */
@RestController
@RequestMapping("word")
@Api(description = "英语单词记事本")
public class EnglishWordNoteBookController
		extends AbstractCurdController<EnglishWordNoteBook, EnglishWordNoteBookFilter, Long> {

	@Autowired
	private IEnglishWordNoteBookService englishWordNoteBookService;

	@PostMapping("add")
	@ApiOperation("创建一个新单词,存在该单词就是修改中文释义,保存单词为小写")
	@SaveOrUpdate(value = "创建一个新单词,存在该单词就是修改中文释义,保存单词为小写", param = { "englishWord", "chineseMeaning" })
	@MyLog
	public ResponseEntity<EnglishWordNoteBookEntity> add(
			@ApiParam(value = "英文单词", required = true, example = "ability") @RequestParam("englishWord") String englishWord,
			@ApiParam(value = "中文释义", required = true, example = "能力,有能力的") @RequestParam("chineseMeaning") String chineseMeaning) {
		EnglishWordNoteBookEntity e = this.englishWordNoteBookService.findByEnglishWord(englishWord);
		if (e != null) {
			e.setEnglishWord(e.getEnglishWord().toLowerCase());
			e.setChineseMeaning(chineseMeaning);
		} else {
			e = new EnglishWordNoteBookEntity();
			e.setChineseMeaning(chineseMeaning);
			e.setEnglishWord(englishWord.toLowerCase());
		}
		e = this.englishWordNoteBookService.save(e);
		return new ResponseEntity<EnglishWordNoteBookEntity>(e, HttpStatus.CREATED);
	}

}
